package com.huobao.yunying.activitys;

import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.simple.eventbus.EventBus;
import org.xutils.view.annotation.ViewInject;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.jjobes.slidedatetimepicker.SlideDateTimeListener;
import com.github.jjobes.slidedatetimepicker.SlideDateTimePicker;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.BitmapTools;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.utils.Utils;
import com.huobao.zhangying.R;
import com.xrz.sxm.base.utils.Log2File;

import android.content.Intent;
import android.graphics.Bitmap;
import android.text.TextUtils;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import me.nereo.multi_image_selector.MultiImageSelectorActivity;
import net.bither.util.NativeUtil;

public class Act_baitiao_shenqing extends HuobaoBaseActivity implements OnClickListener {

	@ViewInject(R.id.edt_storename)
	private EditText edt_storename;
	@ViewInject(R.id.edt_zhucehao)
	private EditText edt_zhucehao;
	@ViewInject(R.id.checkbox)
	private CheckBox checkbox;
	@ViewInject(R.id.tv_time)
	private TextView tv_time;
	@ViewInject(R.id.imageview)
	private ImageView imageview;

	private String mSelectPhoto;
	private String user_id;

	private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

	@Override
	protected int getContentView() {
		// TODO Auto-generated method stub
		return R.layout.layout_baitiao_shenqing;
	}

	@Override
	protected String getTitleText() {
		// TODO Auto-generated method stub
		return getString(R.string.title_1);
	}

	@Override
	protected void init() {
		user_id = getIntent().getStringExtra("id");
		tv_time.setOnClickListener(this);
		imageview.setOnClickListener(this);
	}

	private void showDateDialog(Date date) {
		new SlideDateTimePicker.Builder(getSupportFragmentManager()).setListener(listener).setInitialDate(date).build()
				.show();
	}

	private SlideDateTimeListener listener = new SlideDateTimeListener() {

		@Override
		public void onDateTimeSet(Date date) {
			tv_time.setText(format.format(date));
		}

		@Override
		public void onDateTimeCancel() {
		}
	};

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_time:
			Date date = new Date();
			if (tv_time.getText().toString().equals(getResources().getString(R.string.text1)) == false) {
				try {
					date = format.parse(tv_time.getText().toString());
				} catch (ParseException e) {
				}
			}
			showDateDialog(date);
			break;
		case R.id.imageview:
			onSelectImage();
			break;
		}
	}

	private void onSelectImage() {
		Intent intent = new Intent(this, MultiImageSelectorActivity.class);
		// �Ƿ���ʾ����ͼƬ
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
		// ����ѡ��ͼƬ����
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_COUNT, 1);
		// ѡ��ģʽ
		intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_MULTI);
		// // Ĭ��ѡ��
		// if(mSelectPath != null && mSelectPath.size()>0){
		// intent.putExtra(MultiImageSelectorActivity.EXTRA_DEFAULT_SELECTED_LIST,
		// mSelectPath);
		// }
		startActivityForResult(intent, 1);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == 1) {
			if (resultCode == RESULT_OK) {
				List<String> mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
				if (mSelectPath != null && mSelectPath.size() != 0) {
					mSelectPhoto = mSelectPath.get(0);
					showSelectPic(mSelectPhoto);
				}
			}
		}
	}

	private void showSelectPic(String string) {
		ImageUtils.loadimgFromSDcard(imageview, string);
	}

	public void onSubmitClick(View view) {
		if (mSelectPhoto == null) {
			Toast.makeText(context, "��ѡ��Ӫҵִ����Ƭ", Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(edt_storename.getText().toString())) {
			Toast.makeText(context, "����д�ŵ�����", Toast.LENGTH_SHORT).show();
			return;
		}
		if (TextUtils.isEmpty(edt_zhucehao.getText().toString())) {
			Toast.makeText(context, "����дӪҵִ��ע���", Toast.LENGTH_SHORT).show();
			return;
		}
		if (tv_time.getText().toString().equals("��ѡ����ʱ��") && checkbox.isChecked() == false) {
			Toast.makeText(context, "��ѡ����ʱ��", Toast.LENGTH_SHORT).show();
			return;
		}
		Bitmap bitmap = BitmapTools.createImageThumbnail(mSelectPhoto);
		if (bitmap == null)
			return;
		File f = new File(mSelectPhoto);
		File newPath = new File(f.getParent(), f.getName() + "temp" + ".jpg");
		NativeUtil.compressBitmap(bitmap, 100, newPath.getAbsolutePath(), true);
		bitmap.recycle();
		String base64str = Base64.encodeToString(Utils.File2byte(newPath.getAbsolutePath()), Base64.NO_WRAP);
		Log2File.newInstant().log2File(base64str);
		Map<String, String> map = HttpUtil.getParams("app", "uploadpic", "act", "picupload", "user_id", user_id,
				"portrait", base64str, "type", "6");
		Http.getInstant().sendRequest(context, true, HttpConst.UPLOAD_PHOTO_URL, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				String photo = jsonObject.getString("retval");
				upload(photo);
			}

			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub

			}
		});
	}

	private void upload(String photo) {
		Map<String, String> map = HttpUtil.getParams("app", "baitiao", "act", "apply", "shop_name",
				edt_storename.getText().toString().trim(), "license_code", edt_zhucehao.getText().toString().trim(),
				"long_date", checkbox.isChecked() ? "true" : "", "endtime_regnum",
				tv_time.getText().toString().equals("��ѡ����ʱ��") ? "" : tv_time.getText().toString(), "license_pic",
				photo);
		Http.getInstant().sendRequest(context, map, new HttpListener() {
			
			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				String retval = jsonObject.getString("retval");
				Toast.makeText(context, retval, Toast.LENGTH_SHORT).show();
				EventBus.getDefault().post("", "uploadPhotoSuccess");
				setResult(RESULT_OK);
				finish();
			}
			
			@Override
			public void onHttpError(String error) {
				// TODO Auto-generated method stub
				
			}
		});
	}
}
